﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoginPanel : MonoBehaviour
{
    public static LoginPanel instance;
    private void Awake()
    {
        instance = this;
    }
    public static void Show()
    {
        if (instance)
        {
            instance.gameObject.SetActive(true);
        }
    }
    public static void Hide()
    {
        if (instance)
        {
            instance.gameObject.SetActive(false);
        }
    }
    private void Start()
    {
        LoginPanel.Hide();
    }

    private string inputStr = "";
    private string inputPW = "";

    public Rect rc_win;
    public GUIStyle windowStyle;

    public Rect rc_user_label;
    public Rect rc_user_input;
    public Rect rc_pass_label;
    public Rect rc_pass_input;
    public Rect rc_back_button;
    public Rect rc_login_button;
    public GUIStyle backButtonStyle;
    public GUIStyle loginButtonStyle;
    public GUIStyle labelStyle;

    public int userMaxLength = 10;
    public int passMaxLength = 10;

    private void DrawWindow(int id)
    {
        GUI.Label(rc_user_label, "账号", labelStyle);
        inputStr = GUI.TextField(rc_user_input, inputStr, userMaxLength);
        GUI.Label(rc_pass_label, "密码", labelStyle);
        inputPW = GUI.PasswordField(rc_pass_input, inputPW, '*', passMaxLength);

        if (GUI.Button(rc_back_button, "返回", backButtonStyle))
        {
            LoginPanel.Hide();
        }
        if (GUI.Button(rc_login_button, "登录", loginButtonStyle))
        {
            if (inputStr == "admin" && inputPW == "8888")
            {
                SceneManager.LoadScene("GameScene");
            }
            else
            {
                Debug.LogFormat("账号密码不正确!");
            }
        }
    }

    private void OnGUI()
    {
        GUI.ModalWindow(1, rc_win, DrawWindow, "", windowStyle);
    }
}
